package com.cheer.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cheer.common.Result;
import com.cheer.dto.OaCertDTO;
import com.cheer.dto.OaUserChangeDTO;
import com.cheer.dto.OaUserRegisterDTO;
import com.cheer.dto.OaUserUpdateDTO;
import com.cheer.service.*;
import com.cheer.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;


/**
 * @author cheer
 */
@RestController
@RequestMapping("/auth")
@Api(tags = "授权controller  TODO")
public class AuthController {

    private static final Logger log = LoggerFactory.getLogger(AuthController.class);

    @Autowired
    private AuthService authService;

    @Autowired
    private OaUserService oaUserService;

    @Autowired
    private MallCreditRecordService mallCreditRecordService;

    @Autowired
    private OaLearnClassService oaLearnClassService;

    @GetMapping("/login")
    public Result<String> login(@RequestParam String code, HttpSession session) {
        log.info("auth login code {}", code);
        return Result.success(authService.login(code, session));
    }

    @PostMapping("/register")
    public Result<String> register(@RequestBody @Valid OaUserRegisterDTO userRegisterDTO, HttpSession session) {
        userRegisterDTO.setHeadImgUrl(authService.getHeadImgUrl(session));
        userRegisterDTO.setNickname(authService.getNickname(session));
        userRegisterDTO.setOpenid(authService.getOpenid(session));

        Integer userId = authService.register(userRegisterDTO);
        session.setAttribute("userId", userId);

        return Result.success(authService.getHeadImgUrl(session));
    }

    @GetMapping("/info")
    public Result<OaUserFrontInfoVO> info(HttpSession session) {
        return Result.success(oaUserService.frontInfo(authService.getUserId(session)));
    }

    @GetMapping("/cert")
    public Result<OaCertVO> cert(HttpSession session) {
        return Result.success(oaUserService.cert(authService.getUserId(session)));
    }

    @PostMapping("/cert")
    public Result updateCert(@RequestBody @Valid OaCertDTO oaCertDTO, HttpSession session) {
        oaCertDTO.setUserId(authService.getUserId(session));
        oaUserService.updateCert(oaCertDTO);
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@RequestBody @Valid OaUserUpdateDTO userUpdateDTO, HttpSession session) {
        userUpdateDTO.setId(authService.getUserId(session));
        oaUserService.update(userUpdateDTO);
        return Result.success();
    }

    @PostMapping("/change")
    @ApiOperation("交换账户")
    public Result change(@RequestBody @Valid OaUserChangeDTO userChangeDTO, HttpSession session) {
        oaUserService.change(userChangeDTO, session);
        return Result.success();
    }

    @GetMapping("/transfer")
    @ApiOperation("账户转换")
    public Result transfer(String checkCode, HttpSession session) {
        Integer differ = oaUserService.transfer(checkCode, session);
        return Result.success(differ);
    }

    @GetMapping("/share")
    public Result share(@RequestParam String url) {
        return Result.success(authService.share(url));
    }

    @GetMapping("/sign")
    @ApiOperation("获取签名")
    public Result<ShareTicketVO> sign() {
        return Result.success(authService.sign());
    }

    @GetMapping("/creditRank")
    @ApiOperation("首页-积分排名")
    public Result<CreditRankVO> creditRank(HttpSession session) {
        CreditRankVO rank = oaUserService.getRank(authService.getUserId(session));
        return Result.success(rank);
    }

    @GetMapping("/credit")
    @ApiOperation("积分查询")
    public Result<CreditVO> credit(HttpSession session) {
        return Result.success(oaUserService.getCredit(authService.getUserId(session)));
    }

    @GetMapping("/credit/list")
    public Result<List<CreditDateVO>> listCredit(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                 HttpSession session) {
        return Result.success(mallCreditRecordService.listCredit(pageNum, pageSize, authService.getUserId(session)));
    }

    @GetMapping("/class")
    public Result<List<SimpleObjectVO>> learnClass(@RequestParam Integer id, HttpSession session) {
        return Result.success(oaLearnClassService.sub(id));
    }


}
